package com.ruoyi.mp.mapper;

import com.ruoyi.mp.domain.MpUser;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 公众号粉丝Mapper接口
 *
 * @author ruoyi
 * @date 2023-11-18
 */
public interface MpUserMapper
{
    /**
     * 查询公众号粉丝
     *
     * @param id 公众号粉丝主键
     * @return 公众号粉丝
     */
    public MpUser selectMpUserById(Long id);

    /**
     * 查询公众号粉丝列表
     *
     * @param mpUser 公众号粉丝
     * @return 公众号粉丝集合
     */
    public List<MpUser> selectMpUserList(MpUser mpUser);

    /**
     * 新增公众号粉丝
     *
     * @param mpUser 公众号粉丝
     * @return 结果
     */
    public int insertMpUser(MpUser mpUser);

    /**
     * 修改公众号粉丝
     *
     * @param mpUser 公众号粉丝
     * @return 结果
     */
    public int updateMpUser(MpUser mpUser);

    /**
     * 删除公众号粉丝
     *
     * @param id 公众号粉丝主键
     * @return 结果
     */
    public int deleteMpUserById(Long id);

    /**
     * 批量删除公众号粉丝
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteMpUserByIds(Long[] ids);

    /**
     * 批量删除公众号粉丝
     *
     * @param appId 需要删除的公众号AppId
     */
    @Delete("delete from  mp_user    where app_id = #{appId}")
    public void deleteByAppId(String appId);

    /**
     * 查询粉丝总数
     *
     * @param appId 需要查询的公众号AppId
     */
    @Select("select  count(*) from mp_user where app_id = #{appId} ")
    public int selectCountAppId(String appId);

    @Insert("<script>" +
            "INSERT INTO mp_user (openid,subscribe_status,subscribe_time,nickname,remark,tag_ids,app_id,create_time,update_time)" +
            "VALUES" +
            "<foreach collection=\"mpUsers\" item=\"item\" index=\"index\" separator=\",\">" +
            "(#{item.openid},#{item.subscribeStatus},#{item.subscribeTime},#{item.nickname},#{item.remark},#{item.tagIds},#{item.appId},#{item.createTime},#{item.updateTime})" +
            "</foreach>" +
            "</script>")
    public int batchInsertMpUser(@Param("mpUsers") List<MpUser> mpUsers);

    @Select("<script>" +
            "SELECT openid FROM mp_user WHERE id IN " +
            "<foreach collection='ids' item='item' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    public  List<String>  selectOpenidList(@Param("ids") Long[] ids);


    @Insert("<script>" +
            "INSERT INTO mp_user_black (open_id,app_id) " +
            "VALUES " +
            "<foreach collection=\"openidList\" item=\"item\" index=\"index\" separator=\",\">" +
            "(#{item}, #{appId})" +
            "</foreach>" +
            "</script>")
    public  int pushToBlacklist(@Param("openidList")  List<String> openidList,@Param("appId") String appId);

    @Delete("<script>" +
            "delete from mp_user_black WHERE open_id IN (" +
            "<foreach collection=\"openidList\" item=\"item\" index=\"index\" separator=\",\">" +
            "#{item}" +
            "</foreach>" +
            " )  and app_id = #{appId}"+
            "</script>")
    public int pullFromBlacklist(@Param("openidList")  List<String> openidList,@Param("appId") String appId);

    @Delete("delete from mp_user_black where app_id = #{appId}")
    public void deleteMpUserBlackByAppid(String appId);
}
